import type { PageResult, Result } from "#/api";
import type { Dict, DictItem } from "#/entity";
import apiClient from "../../apiClient";

export interface PageListReq {
	dictCode?: string;
	dictName?: string;
	page: number;
	pageSize: number;
}

//刷新字典缓存
const refreshCache = () => apiClient.get<Result>({ url: "/sys/dictItem/refreshCache" })

//生成枚举代码
const generateEnum = (id: number) => apiClient.get<Result>({url: `/sys/dict/generateEnum/${id}`});

const findById = (id: number) => apiClient.get<Dict>({ url: `/sys/dict/info/${id}` });
const pageList = (data: PageListReq) => apiClient.post<PageResult<Dict>>({ url: '/sys/dict/page', data });
const add = (data: Dict) => apiClient.post<Dict>({ url: "/sys/dict/add", data });
const update = (data: Dict) => apiClient.post<Dict>({ url: "/sys/dict/update", data });
const deleteDict = (ids: string[]) => apiClient.delete({ url: "/sys/dict/del", params: { ids: ids.join(",") } });

const listAll = () => apiClient.get<Dict[]>({url:"/sys/dict/all"})

const itemList = (id: string|number) => apiClient.get<Result>({url: `/sys/dictItem/list/${id}`});
const addItem = (data: DictItem) => apiClient.post<Dict>({ url: "/sys/dictItem/add", data });
const updateItem = (data: DictItem) => apiClient.post<Dict>({ url: "/sys/dictItem/update", data });
const deleteItem = (ids: string[]) => apiClient.delete({ url: "/sys/dictItem/del", params: { ids: ids.join(",") } });
const getItemByCode = (code: string) => apiClient.get<Result>({url: `/sys/dictItem/listByCode/${code}`});

export default {
	refreshCache,
	generateEnum,
	findById,
	pageList,
	add,
	update,
	deleteDict,
  itemList,
  addItem,
  updateItem,
  deleteItem,
	getItemByCode,
	listAll
};
